#include "includes/house.h"
#include "includes/util.h"

void house_init(House * house){
	Parallelepiped * p = &house->param;
	p->height = 5.;
	p->length = 12.;
	p->width = 8.;
	
	Parallelepiped * c = &house->chimney;
	c->height = 2.;
	c->length = 1.;
	c->width = 1.;
	
	house->texture_top = util_load_texture("./images/see/toit.png");
	
	c->texture1 = c->texture2 = c->texture3 = c->texture4 = house->texture_top;
	p->texture1 = p->texture2 = p->texture3 = p->texture4 = util_load_texture("./images/see/bois.png");
	
	parallelepiped_init(&house->param);
	parallelepiped_init(&house->chimney);
	
	house->top = glGenLists(2);
	house->sides = house->top + 1;
	glNewList(house->top, GL_COMPILE);
		glBegin(GL_QUADS);
			glTexCoord2f(0, 1);		glVertex3f(0., - HOUSE_TOP_OVERSIZE, p->height - HOUSE_TOP_OVERSIZE);
			glTexCoord2f(1, 1);		glVertex3f(p->length, - HOUSE_TOP_OVERSIZE, p->height - HOUSE_TOP_OVERSIZE);
			glTexCoord2f(1, 0);		glVertex3f(p->length, p->width / 2., p->height + p->width / 2.);
			glTexCoord2f(0, 0);		glVertex3f(0., p->width / 2., p->height + p->width / 2.);
			
			glTexCoord2f(1, 0);		glVertex3f(0., p->width + HOUSE_TOP_OVERSIZE, p->height - HOUSE_TOP_OVERSIZE);
			glTexCoord2f(0, 0);		glVertex3f(p->length, p->width + HOUSE_TOP_OVERSIZE, p->height - HOUSE_TOP_OVERSIZE);
			glTexCoord2f(0, 1);		glVertex3f(p->length, p->width / 2., p->height + p->width / 2.);
			glTexCoord2f(1, 1);		glVertex3f(0., p->width / 2., p->height + p->width / 2.);
		glEnd();
	glEndList();
	
	glNewList(house->sides, GL_COMPILE);
		glBegin(GL_TRIANGLES);
			glTexCoord2f(0, 1);		glVertex3f(0., 0., p->height);
			glTexCoord2f(1, 1);		glVertex3f(0., p->width, p->height);
			glTexCoord2f(.5, .5);	glVertex3f(0., p->width / 2., p->height + p->width / 2.);
			
			glTexCoord2f(0, 1);		glVertex3f(p->length, 0., p->height);
			glTexCoord2f(1, 1);		glVertex3f(p->length, p->width, p->height);
			glTexCoord2f(.5, .5);	glVertex3f(p->length, p->width / 2., p->height + p->width / 2.);
		glEnd();
	glEndList();
	
}

void house_draw(House * house){
	glPushMatrix();
	glTranslatef(0., 0., 0.);
	parallelepiped_draw(&house->param);
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);
	
	glBindTexture(GL_TEXTURE_2D, house->param.texture1);
	glCallList(house->sides);
	
	glBindTexture(GL_TEXTURE_2D, house->texture_top);
	glCallList(house->top);
	
 	glTranslatef(2., 1., house->param.height + 1.);
	
	glColor3f(.7, 0., 0.);
	parallelepiped_draw(&house->chimney);
	glColor3f(1., 1., 1.);
	
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
	
	glPopMatrix();
}